package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.common.PostDubbleEvent;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.common.QueryUserUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewOrderRetSignCmd;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.utils.UserInformation;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewOrderRetSignCmdImpl extends CommandAdapter implements ViewOrderRetSignCmd {

    @Autowired
    QueryCpUtil postQueryEvemt;
    @Autowired
    QueryUserUtil postUserEvent;
    @Autowired
    PostDubbleEvent postDubbleEvent;

    private Logger accesLogger = LoggerFactory.getLogger(Constants.ACCESS_PARAM_LOGGER);

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewOrderRetSignCmdImpl--param-->{}",param);

        if (null == param) {
            throw new NDSException("参数不能为空！");
        }
        log.debug("ViewOrderRetSignCmdImpl--param-->{}",param);
//        log.debug("------ 参数-------" + param);
        ValueHolder vh = new ValueHolder();
        JSONArray dataArray = param.getJSONArray("data");

        JSONArray succArray = new JSONArray();
        JSONArray failArray = new JSONArray();

        for (int i = 0; i < dataArray.size(); i++) {

            JSONObject dataJo = dataArray.getJSONObject(i);

            try {

                User user = postUserEvent.getUser(UserInformation.USER_NAME);
                String notic = "com.jackrain.nea.dl.tran.api.BtranMergeSigninCmd:1.0:dl";
                QuerySessionImpl querySession = new QuerySessionImpl(user);
                DefaultWebEvent event = new DefaultWebEvent("dlMergeSignin", new HashMap(16));
                event.put("param", dataJo);
                querySession.setEvent(event);
                accesLogger.info("ViewOrderRetSignCmdImpl--dataJo-->{}",dataJo);
//                log.debug("-------批量签收参数------->" + dataJo);
                JSONObject retJo = postDubbleEvent.executePostDubble(notic, querySession);
//                log.debug("-------返回参数---------->");
                accesLogger.info("ViewOrderRetSignCmdImpl--retJo-->{}",retJo);

                if (retJo.get("code") != null && (int) retJo.get("code") < 0) {
                    throw new NDSException("订单批量签收失败:" + retJo.get("message") + retJo.get("error") + retJo.get("data"));
                }
                succArray.add(retJo.get("data"));

            } catch (Exception e) {
                failArray.add(e.getMessage());
            }
        }
        if (succArray.size() > 0) {

            vh.put("successdata", succArray);
            vh.put("code", 0);
            vh.put("message", "签收成功！");
        }
        if (failArray.size() > 0) {
            vh.put("faildata", failArray);
            vh.put("code", -1);
            vh.put("message", "签收失败！");
        }
        return vh;
    }
}